;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.SluggishShard", [])
        .controller("SluggishShardCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "EventBus", "$filter", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, EventBus, $filter, UserContextService) {

            $scope.handleBlur = function(e) {
                $scope.condition.materialNos = UserContextService.MultiInput(e.target.value);
            };

            $scope.exportParam = {
                sectionParam: {ids: []},
                sectionUrl: ybHttp.RequestUrl("device", "/sluggishShard/export"),
                allUrl: ybHttp.RequestUrl("device", "/sluggishShard/exportAll")
            };
            $scope.selectSingle = function (item) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    selectAll = checkAll($scope.dataList);
                }
                $scope.markInfo.allChecked = selectAll;
                getIds();
            };
            $scope.selectAll = function () {
                for (var i = 0; i < $scope.dataList.length; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
                getIds();
            };
            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }
            function getIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.toolbarConfig = {
                showAdvanceSearch: false
            };
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                    return;
                }
                $timeout(function () {
                    $scope.toolbarConfig["show" + toolbar] = true;
                });
            };
            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.condition = {};
            $scope.activeFlag = [true, false, false, false, false];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.pageInfoForAdd = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.markInfo = {
                curRow: -1,
                allChecked: false
            };
            $scope.dataList = [];
            $scope.dataListForAdd = [];
            $scope.tbConf = {
                fixedWidth: 120,
                scrollWidth: 1200,
                tableCol: [
                    {
                        type: "",
                        name: "代理商名称",
                        dataField: "orgName",
                        width: 300,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialNo",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialName",
                        width: 300,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "滞销数量",
                        dataField: "unsalableQty",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "滞销单价",
                        dataField: "unsalablePrice",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "operate",
                        name: "操作",
                        width: 80,
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            $scope.tbConfForAdd = {
                fixedWidth: 60,
                scrollWidth: 1200,
                tableCol: [
                    {
                        type: "",
                        name: "代理商名称",
                        dataField: "orgName",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialNo",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialName",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "库存数量",
                        dataField: "amount",
                        width: 200,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "滞销数量",
                        dataField: "unsalableQty",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        needInput: true,
                        isQty: true
                    },
                    {
                        type: "",
                        name: "滞销单价",
                        dataField: "unsalablePrice",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                        needInput: true,
                        isPrice: true
                    },
                    {
                        type: "operate",
                        name: "操作",
                        width: 60,
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: { left: $scope.tbConf.fixedWidth + "px" }
                };
                $scope.tbStyleForAdd = {
                    scrollLeft: { left: $scope.tbConfForAdd.fixedWidth + "px" }
                };
            };
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };
            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false];
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        initData();
                        getDataList();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };
            $scope.delData = function (item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/sluggishShard/delete";
                    ybHttp.Post(service, reqUrl, item).then(function (data) {
                        if (data.code !== 0) {
                            ybWidget.ShowMessage(data.message);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                })
            };
            $scope.addData = function () {
                getDataListForAdd();
            };
            //一键清空
            $scope.batchDel = function () {
                ybWidget.ShowMessage("确认要一键清空？", MESG_TYPE.Ask).then(function (data) {
                    var service = "device";
                    var reqUrl = "/sluggishShard/batchDel";
                    ybHttp.Get(service, reqUrl).then(function (data) {
                        if (data.code !== 0) {
                            ybWidget.ShowMessage(data.message);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                })
            };
            $scope.searchForAdd = function (curpage) {
                getDataListForAdd();
            };

            $scope.editData = function (id) {
                $scope.currentId = id;
                $scope.changeTab(2);
            };

            $scope.saveData = function (item, operationName) {
                angular.forEach($scope.dataListForAdd, function(item) {
                    item.showErr = false;
                    item.showPriceErr = false;
                });
                $scope.checkQty(item);
                $scope.checkPrice(item);
                if (item.qtyErr || item.qtyCheckErr || item.priceErr) return

                var service = "device";
                var reqUrl = "/sluggishShard/add";
                if (operationName === 'edit') {
                    reqUrl = "/sluggishShard/update";
                }
                ybHttp.Post(service, reqUrl, item).then(function (data) {
                    if (data.code !== 0) {
                        ybWidget.ShowMessage(data.message);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0)
                });
            };

            function initData() {
                $scope.exportParam.sectionParam.ids = [];
                $scope.condition = {};
                $scope.markInfo = {
                    curRow: -1,
                    allChecked: false
                };
                $scope.pageInfo = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
                $scope.pageInfoForAdd = {
                    totalPages: 1,
                    totalRows: 0,
                    pageSize: 10,
                    curPage: 1
                };
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/sluggishShard/list";
                var param = {
                    filters: angular.copy($scope.condition)
                };
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }
            function getDataListForAdd(curPage) {
                var service = "device";
                var reqUrl = "/sluggishShard/getInsertList";
                var param = {
                    filters: {}
                };
                param.pageSize = $scope.pageInfoForAdd.pageSize;
                if (!!curPage) {
                    $scope.pageInfoForAdd.curPage = curPage;
                }
                param.pageNum = $scope.pageInfoForAdd.curPage;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfoForAdd.totalRows = resp.value.total;
                    $scope.pageInfoForAdd.totalPages = resp.value.pages;
                    $scope.dataListForAdd = resp.value.list;
                    $scope.changeTab(1);
                });
            }

            $scope.showQtyTip = function (item) {
                if (!!item.qtyErr || !!item.qtyCheckErr) {
                    item.showErr = true;
                }
            };

            $scope.hideQtyTip = function (item) {
                item.showErr = false;
            };

            $scope.checkQty = function (item) {
                item.qtyErr = false;
                item.qtyCheckErr = false;
                if (!item.unsalableQty || parseFloat(item.unsalableQty) <= 0) {
                    item.qtyErr = true;
                    item.showErr = true;
                    return;
                }
                if (item.unsalableQty > item.amount) {
                    item.qtyCheckErr = true;
                    item.showErr = true;
                    return;
                }
            };

            $scope.showPriceTip = function (item) {
                if (!!item.priceErr) {
                    item.showPriceErr = true;
                }
            };

            $scope.hidePriceTip = function (item) {
                item.showPriceErr = false;
            };

            $scope.checkPrice = function (item) {
                item.priceErr = false;
                if (!item.unsalablePrice || parseFloat(item.unsalablePrice) <= 0) {
                    item.priceErr = true;
                    item.showPriceErr = true;
                    return;
                }
            };

            $scope.importData = function () {
                var param = {
                    name: "呆滞零件",
                    templateReqService: "device",
                    templateReqUrl: "/sluggishShard/downloadImportExcel", // 模板路径
                    importReqService: "device",
                    importReqUrl: "/sluggishShard/importExcel", // 导入接口
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    getDataList(1);
                });
            };

            $scope.btnPermission = {
                add: true,
                edit: true,
                delete: true,
                import: true,
                export: true,
                batchDel: true,
            };
            var btnPermissionCode = {
                add: window.ActionCode.sluggishShardAdd,
                edit: window.ActionCode.sluggishShardEdit,
                delete: window.ActionCode.sluggishShardDelete,
                import: window.ActionCode.sluggishShardImport,
                export: window.ActionCode.sluggishShardExport,
                batchDel: window.ActionCode.sluggishShardBatchDel
            };
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
        ;
})(window, window.angular);
